var $form;
var form;
var $;
layui.config({
    base: "../../js/"
}).use(['form', 'layer', 'upload', 'laydate'], function () {
    form = layui.form();
    var layer = parent.layer === undefined ? layui.layer : parent.layer;
    $ = layui.jquery;
    $form = $('form');
    var laydate = layui.laydate;

    var filedir = "activity/"; // 对应oss目录


    $('#resetBtn').click(function(){
        window.location.reload();
    });

   /* upload({
        url: "../../json/userface.json",
        success: function (res) {
            var num = parseInt(4 * Math.random());  //生成0-4的随机数
            //随机显示一个头像信息
            userFace.src = res.data[num].src;
            window.sessionStorage.setItem('userFace', res.data[num].src);
        }
    });

    //判断是否修改过头像，如果修改过则显示修改后的头像，否则显示默认头像
   if (window.sessionStorage.getItem('userFace')) {
        $("#userFace").attr("src", window.sessionStorage.getItem('userFace'));
    } else {
        $("#userFace").attr("src", "../../images/face.jpg");
    }*/

    //提交个人资料
    $("#submitActivity").click(function(e){
        e.preventDefault();
        e.stopPropagation();
        // var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //将填写的用户信息存到session以便下次调取
        var activityAddForm = $('#activityAddForm');
        if(!checkImgUpload()) return false;
        var data = activityAddForm.serialize();

        $.ajax({
            cache: false,
            type: 'POST',
            url: "/activity/addActivity",
            dataType: "json",
            data: data,
            async: false,
            success: function (data) {
                if (data.errType === "0000") {
                    top.layer.alert("保存成功", {icon: 1});
                    setTimeout(function () {
                        var pIndex = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(pIndex); //再执行关闭
                        parent.window.location.reload();
                    },1000)

                } else {
                    top.layer.alert("保存异常,请重试", {icon: 0});
                }
            },
            error: function () {
                top.layer.alert("发生异常,请重试", {icon: 2});
            }
        });
        return false;
    });


    initFroalaEditor();
    function initFroalaEditor() {
        $('#froalaEditor').editable({
            inlineMode: false,
            fontList: ["宋体", "仿宋", "楷体", "微软雅黑", "Arial,Helvetica", "Courier,Courier New", "Georgia", "Times New Roman,Times", "Trebuchet MS", "Verdana,Geneva"],
            theme: 'gray',
            //模版
            height: '400px', //高度
            maxHeight: "500px",
            width: '50%',
            alwaysBlank: true,
            language: "zh_cn",
            direction: "ltr",
            allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
            autosave: true,
            autosaveInterval: 2500,
            saveParams: {postId: "123"},
            spellcheck: true,
            plainPaste: true,
            imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
            imageUploadURL: '/oss/ossImgUpload?filedir=' + filedir + '&water=1'
        });
    }

    var imgUrlArr = [];
    initCoverImgUpload();
    // 封面图上传初始化

    function initCoverImgUpload() {
        $.Tupload.init({
            url: '/oss/ossImgUpload?filedir='+filedir,
            title: "封面图大小不能超过300kb<span style='color:red;'>(*第一张将被作为活动列表封面；第二张将被作为活动内容图片)<br/>-其中封面图分辨率接近3*5为佳-</span>",
            fileNum: 2, // 上传文件数量
            divId: "T_upload", // div  id
            accept: "image/jpeg,image/x-png,image/jpg", // 上传文件的类型
            fileSize:300,     // 上传文件的大小 kb
            filedir:filedir, // 图片上传至oss对应的目录
            onSuccess: function(data, i) {
                console.log(i+">>>>>>>>"+JSON.stringify(data));
                imgUrlArr[i] = data["link"];
                if(imgUrlArr.length >1)
                    $("#coverUrl").val(imgUrlArr.join("@@@@"));
                else
                    $("#coverUrl").val(data["link"]);

            },
            onDelete: function(i) {

            }
        });
    }


    // 封面图上传是否达3张
    function checkImgUpload(){

        if(imgUrlArr.length < 1){
            top.layer.alert("封面图必须上传一张以上",{icon:0});
            return false;
        }
        return true;
    }


});

